/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.admin.dal.mongo.mongo;

import java.util.List;

import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.damc.admin.dal.common.PageList;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.LogDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.dateobject.LogDO;

/**
 * @author icanci
 * @since 1.0 Created in 2025/05/16 23:01
 */
@Service("logDAO")
public class MongoLogDAO extends AbstractBaseDAO<LogDO> implements LogDAO {
    @Override
    public void insert(LogDO logDO) {
        super.insert(logDO);
        mongoTemplate.insert(logDO, COLLECTION_NAME);
    }

    @Override
    public void update(LogDO logDO) {
        super.update(logDO);
        mongoTemplate.save(logDO, COLLECTION_NAME);
    }

    /**
     * 查询文档所有记录
     *
     * @return 返回查询的结果
     */
    @Override
    public List<LogDO> queryAll() {
        Criteria criteria = Criteria.where(LogColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    /**
     * 查询文档所有记录
     *
     * @param logDO    请求参数
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return 返回查询的结果
     */
    @Override
    public PageList<LogDO> pageQuery(LogDO logDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(LogColumn.ENV).is(DEFAULT_ENV);
        // 查询参数
        // 不分区大小写查询，其中操作符"i"：表示不分区大小写
        criteria.and(LogColumn.MODULE).is(logDO.getModule());
        // 不分区大小写查询，其中操作符"i"：表示不分区大小写
        criteria.and(LogColumn.TARGET_ID).is(logDO.getTargetId());
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, LogColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);

    }

    /**
     * 根据 _id 查询一条信息
     *
     * @param _id _id
     * @return 返回查询的结果
     */
    @Override
    public LogDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(LogColumn._ID).is(_id);
        criteria.and(LogColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);

    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return LogColumn.class;
    }
}
